package com.li.senbiao.serial.bean;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class TestJDKBean {
	public static void main(String[] args) {
		TestJDKBean ts = new TestJDKBean();
		
		JDKBean jdkBean = new JDKBean();
		jdkBean.setIntVar(2);
		jdkBean.setStr("hello");
		ts.objectToFile(jdkBean);

		JDKBean newJDKBean = ts.FileToObject();
		System.out.println("intVar:" + newJDKBean.getIntVar());
		System.out.println("strVar:" + newJDKBean.getStr());
	}

	public void objectToFile(JDKBean ts) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream("JDKBean");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(ts);
			oos.flush();
			oos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public JDKBean FileToObject() {
		FileInputStream fis;
		JDKBean ts = null;
		try {
			fis = new FileInputStream("JDKBean");

			ObjectInputStream oin = new ObjectInputStream(fis);
			ts = (JDKBean) oin.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ts;
	}
}
